package problem114;

//114.二叉树展开为链表
//https://leetcode.cn/problems/flatten-binary-tree-to-linked-list/

import java.util.ArrayList;
import java.util.List;

class Solution {
    public void flatten(TreeNode root) {
        List<TreeNode> list = new ArrayList<>();
        preOrder(root, list);
        for(int i = 0; i<list.size(); i++) {
            System.out.print(list.get(i).val + " ");
        }
        TreeNode cur = root;
        for(int i = 1; i<list.size(); i++) {
            cur.right = list.get(i);
            cur.left = null;
            cur = cur.right;
        }
    }
    public void preOrder(TreeNode root, List<TreeNode> list) {
        if(root == null) return;
        list.add(root);
        preOrder(root.left, list);
        preOrder(root.right, list);
    }
}